package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.common.core.util.IdWorker;
import com.alisls.shequtao.service.goods.dao.SpecGroupDAO;
import com.alisls.shequtao.service.goods.dao.SpecParamDAO;
import com.alisls.shequtao.service.goods.dto.SpecGroupDTO;
import com.alisls.shequtao.service.goods.dto.SpecParamDTO;
import com.alisls.shequtao.service.goods.entity.SpecGroupDO;
import com.alisls.shequtao.service.goods.entity.SpecParamDO;
import com.alisls.shequtao.service.goods.service.CategoryService;
import com.alisls.shequtao.service.goods.service.SpecGroupService;
import com.alisls.shequtao.service.goods.service.SpecParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @author Ke Wang
 */
@Service("specParamService")
@AllArgsConstructor
@Slf4j
public class SpecParamServiceImpl extends ServiceImpl<SpecParamDAO, SpecParamDO> implements SpecParamService {

    private final IdWorker idWorker;

    private final SpecParamDAO specParamDAO;

    private final SpecGroupDAO specGroupDAO;

    private final SpecGroupService specGroupService;

    private final CategoryService categoryService;

    @Override
    public List<SpecParamDTO> listSpecParamsByCategoryIdAndSpecGroupId(Long categoryId, Long specGroupId) {
        QueryWrapper<SpecParamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("spec_group_id", specGroupId);
        List<SpecParamDO> specParamDOs = specParamDAO.selectList(queryWrapper);
        return convertToDTOs(specParamDOs);
    }

    @Override
    public List<SpecParamDTO> getSpecParamsBySpecGroupId(Long specGroupId) {
        List<SpecParamDO> specParamDOs = specParamDAO.getSpecParamsBySpecGroupId(specGroupId);
        if (CollectionUtils.isEmpty(specParamDOs)) {
            return null;
        }

        List<SpecParamDTO> specParamDTOs = new ArrayList<>();
        for (SpecParamDO specParamDO: specParamDOs) {
            SpecParamDTO specParamDTO = new SpecParamDTO();
            BeanUtils.copyProperties(specParamDO, specParamDTO);

            SpecGroupDO specGroupDO = specGroupDAO.selectById(specParamDO.getSpecGroupId());
            if (specGroupDO != null) {
                SpecGroupDTO specGroupDTO = new SpecGroupDTO();
                BeanUtils.copyProperties(specGroupDO, specGroupDTO);
                specParamDTO.setSpecGroupDTO(specGroupDTO);
            }
            specParamDTOs.add(specParamDTO);
        }

        return specParamDTOs;
    }

    @Override
    public void saveSpecParamWithSpecGroup(SpecParamDTO specParamDTO) {
        SpecParamDO specParamDO = new SpecParamDO();
        BeanUtils.copyProperties(specParamDTO, specParamDO);
        specParamDO.setId(idWorker.nextId());

        SpecGroupDTO specGroupDTO = specParamDTO.getSpecGroupDTO();
        if (specGroupDTO != null) {
            SpecGroupDO specGroupDO = new SpecGroupDO();
            BeanUtils.copyProperties(specGroupDTO, specGroupDO);
            specParamDO.setSpecGroupId(specGroupDO.getCategoryId());
        }

        specParamDAO.saveSpecParamWithSpecGroup(specParamDO);
    }

    @Override
    public SpecParamDTO convertToDTO(SpecParamDO specParamDO) {
        if (specParamDO == null) {
            return null;
        }

        SpecParamDTO specParamDTO = new SpecParamDTO();
        BeanUtils.copyProperties(specParamDO, specParamDTO);
        Long categoryId = specParamDO.getCategoryId();
        if (categoryId != null) {
            specParamDTO.setCategoryDTO(categoryService.convertToDTO(categoryService.getById(categoryId)));
        }
        Long specGroupId = specParamDO.getSpecGroupId();
        if (specGroupId != null) {
            specParamDTO.setSpecGroupDTO(specGroupService.convertToDTO(specGroupDAO.selectById(specGroupId)));
        }
        return specParamDTO;
    }

    @Override
    public SpecParamDO convertToDO(SpecParamDTO specParamDTO) {
        if (specParamDTO == null) {
            return null;
        }

        SpecParamDO specParamDO = new SpecParamDO();
        BeanUtils.copyProperties(specParamDO, specParamDTO);
        if (specParamDTO.getCategoryDTO() != null) {
            specParamDO.setCategoryId(specParamDTO.getCategoryDTO().getId());
        }
        if (specParamDTO.getSpecGroupDTO() != null) {
            specParamDO.setSpecGroupId(specParamDTO.getSpecGroupDTO().getId());
        }
        return specParamDO;
    }

    public List<SpecParamDTO> convertToDTOs(List<SpecParamDO> specParamDOs) {
        if (CollectionUtils.isEmpty(specParamDOs)) {
            return null;
        }

        return specParamDOs.stream().map(this::convertToDTO).collect(Collectors.toList());
    }
}
